﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.WPF.Theme.AdditionalAttributes.ComboBox
 * 唯一标识：47f56c0e-b2ba-4c86-8bd4-d11b74c8e9a8
 * 文件名：ComboBoxAttach
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2025/6/26 22:30:42
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// 
/// </summary>
public static class ComboBoxAttach
{


    public static ICommand GetSelectionChangedCommand(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(SelectionChangedCommandProperty);
    }

    public static void SetSelectionChangedCommand(DependencyObject obj, ICommand value)
    {
        obj.SetValue(SelectionChangedCommandProperty, value);
    }

    // Using a DependencyProperty as the backing store for SelectionChangedCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectionChangedCommandProperty =
        DependencyProperty.RegisterAttached("SelectionChangedCommand", typeof(ICommand), typeof(ComboBoxAttach), new PropertyMetadata(null, OnSelectionChangedCommandCallback));

    private static void OnSelectionChangedCommandCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is ComboBox comboBox)
        {
            // 注册SelectionChanged事件
            comboBox.SelectionChanged -= OnSelectionChanged;
            comboBox.SelectionChanged += OnSelectionChanged;
        }
    }

    /// <summary>
    /// SelectionChanged事件处理方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // 如果sender是ComboBox
        if (sender is ComboBox comboBox)
        {
            // 获取绑定的Command
            var command = GetSelectionChangedCommand(comboBox);
            // 执行Command
            command?.Execute(comboBox.SelectedItem);
        }
    }
}